import 'package:dio/dio.dart';
import 'package:schedule_self_client/api/schedule_self_server/task.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_category_list_resp.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_detail_resp.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_execute_record.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_list_resp.dart';
import 'package:schedule_self_client/api/schedule_self_server/task_list_resp_v2.dart';
import 'package:schedule_self_client/common/api_common.dart';
import 'package:schedule_self_client/common/simple_resp.dart';

class TaskApi {
  static final Dio _dio = Dio(BaseOptions(
    baseUrl: ApiCommon.baseUrl,
    headers: {},
  ));

  static init(InterceptorsWrapper interceptorsWrapper) {
    _dio.interceptors.add(interceptorsWrapper);
  }

  static Future<TaskListResp> getTasks(int pageIndex, int pageSize) async {
    Response response = await _dio.get('/task/v1/getTasks',
        queryParameters: {"pageIndex": pageIndex, "pageSize": pageSize});
    return TaskListResp.fromJson(response.data);
  }

  static Future<TaskListRespV2> getTasksV2(
      {int? completeFlag, String? startTime, String? endTime}) async {
    Response response = await _dio.post('/task/v2/getTasks', data: {
      "completeFlag": completeFlag,
      "startTimeStr": startTime ?? '',
      "endTimeStr": endTime ?? ''
    });
    return TaskListRespV2.fromJson(response.data);
  }

  static Future<SimpleResp> taskCompleteFlagChange(int id) async {
    Response response = await _dio.post('/task/v1/taskCompleteFlagChange',
        options: Options(contentType: "application/json"),
        data: {
          "id": id,
        });
    return SimpleResp.fromJson(response.data);
  }

  static Future<SimpleResp> taskAdd(
      {required String name,
      String? description,
      String? deadline,
      int? taskCategoryId}) async {
    Response response = await _dio.post('/task/v1/createTask', data: {
      'name': name,
      'description': description,
      'deadline': deadline,
      'categoryId': taskCategoryId,
    });
    return SimpleResp.fromJson(response.data);
  }

  static Future<SimpleResp> batchDeleteTasks(List<int> idList) async {
    Response response =
        await _dio.post('/task/v1/batchDeleteTask', data: {'idList': idList});
    return SimpleResp.fromJson(response.data);
  }

  static Future<SimpleResp?> taskExecuteRecord(int taskId, String taskName,
      int recordBy, String startTime, String endTime) async {
    Response response = await _dio.post('/task/v1/saveExecuteRecord',
        options: Options(contentType: 'application/json'),
        data: {
          'taskId': taskId,
          'taskName': taskName,
          'recordBy': recordBy,
          'startTime': startTime,
          'endTime': endTime
        });
    return SimpleResp.fromJson(response.data);
  }

  static Future<TaskExecuteRecordListResp> getTaskExecuteRecordList(
      String startTime, String endTime) async {
    Response response = await _dio.post('/task/v1/getTaskExecuteRecords',
        options: Options(contentType: 'application/json'),
        data: {'startTimeStr': startTime, 'endTimeStr': endTime});
    return TaskExecuteRecordListResp.fromJson(response.data);
  }

  static Future<TaskDetailResp> getTaskDetail(int taskId) async {
    Response response =
        await _dio.get('/task/v1/getTask', queryParameters: {'id': taskId});
    return TaskDetailResp.fromJson(response.data);
  }

  static Future<SimpleResp> updateTask(Task task) async {
    Response response = await _dio.post('/task/v1/updateTask',
        options: Options(contentType: 'application/json'), data: task);
    return SimpleResp.fromJson(response.data);
  }

  static Future<TaskCategoryListResp> getTaskCategoryList() async {
    Response response = await _dio.get('/task/v1/queryTaskCategories');
    return TaskCategoryListResp.fromJson(response.data);
  }
}
